package yhao.demo.service.erp.workflow.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yhao.demo.service.erp.workflow.apilist.form.task.*;
import yhao.demo.service.erp.workflow.apilist.model.flow.FlowNodeModel;
import yhao.demo.service.erp.workflow.apilist.model.task.TaskModel;
import yhao.demo.service.erp.workflow.dao.TaskDao;
import yhao.demo.service.erp.workflow.service.pvm.ProcessSearcher;
import yhao.demo.service.erp.workflow.service.task.*;
import yhao.infra.apilist.validate.IdForm;
import yhao.infra.common.model.Pagination;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TaskService {
    @Autowired
    private ProcessSearcher processSearcher;
    @Resource
    private TaskDao taskDao;
    /**
     * 启动任务
     * @param form
     * @return
     */
    @Transactional
    public TaskStartForm startNewTask(TaskStartForm form) {
        StartTaskCommand command = new StartTaskCommand(taskDao,processSearcher,form);
        command.doCommand();
        return form;
    }

    /**
     * 终止任务
     * @param form
     * @return
     */
    @Transactional
    public IdForm stopTask(IdForm form) {
        StopTaskCommand command = new StopTaskCommand(taskDao,processSearcher,form);
        command.doCommand();
        return form;
    }

    /**
     * 查询任务
     * @param form
     * @return
     */
    public TaskModel pickTask(IdForm form){
        TaskModel task = taskDao.pickTaskById(form.getId());
        List<FlowNodeModel> nextList = processSearcher.findNextNodes(task.getProcessId(),task.getCurrentNodeId());
        List<FlowNodeModel> previousList = processSearcher.findPreviousNodes(task.getProcessId(),task.getCurrentNodeId());
        task.setNextList(nextList);
        task.setPreviousList(previousList);
        TaskNodeItemQueryForm queryForm = new TaskNodeItemQueryForm();
        queryForm.setTaskId(task.getId());
        queryForm.setNodeId(task.getCurrentNodeId());
        task.setItemList(taskDao.queryTaskNodeItem(queryForm));
        return task;
    }

    /**
     * 执行任务，仅更新事项
     * @param form
     * @return
     */
    @Transactional
    public boolean dealTask(TaskDealForm form) {
        DealTaskCommand command = new DealTaskCommand(taskDao,processSearcher,form);
        command.doCommand();
        return true;
    }

    /**
     * 审核通过任务
     * @param form
     * @return
     */
    @Transactional
    public boolean passTask(TaskDealPassForm form) {
        PassTaskCommand command = new PassTaskCommand(taskDao,processSearcher,form);
        command.doCommand();
        return true;
    }

    /**
     * 审核拒绝任务
     * @param form
     * @return
     */
    @Transactional
    public boolean refuseTask(TaskDealRefuseForm form) {
        RefuseTaskCommand command = new RefuseTaskCommand(taskDao,processSearcher,form);
        command.doCommand();
        return true;
    }

    /**
     * 查询任务列表
     * @param form
     * @return
     */
    public Pagination<TaskModel> queryTask(TaskQueryForm form) {
        Pagination<TaskModel> page = new Pagination<>(form.getPageSize(),form.getCurrentPage());
        taskDao.queryTask(page,form);
        return page;
    }
}
